package server;

import java.sql.ResultSet;
import java.sql.SQLException;

public class BankWanterList {
private Data data;
	
	public BankWanterList(){
		data=new Data();
	}
	
	public boolean addWanter(int ID,String name,int key){
		//添加一个应聘者
		String sql;
		int re=0;
		BankAccList acclist=new BankAccList();
		int caNum=acclist.getCANum(ID);
		
		sql="insert into bankwanterlist values (null,"+ID+","+caNum+","+key+",'"+name+"')";
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	
	public boolean searchID(int pID){
		//在应聘者表中检索ID是否存在，存在则返回true
		String sql;
		ResultSet rs=null;
		
		sql="select * from bankwanterlist where pID=" + pID;
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public boolean deleteWanter(int pID){
		//从应聘者表中删除ID为pID的项
		String sql;
		int re=0;
		
		sql="delete from bankwanterlist where pID="+pID;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	
	public String getName(int pID){
		String sql;
		ResultSet rs=null;
		String name="";
		
		sql="select * from bankwanterlist where pID=" + pID+" limit 1";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				name=rs.getString("name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return name;
	}
	
	public int getKey(int pID){
		String sql;
		ResultSet rs=null;
		int key=0;
		
		sql="select * from bankwanterlist where pID=" + pID+" limit 1";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				key=rs.getInt("enterkey");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return key;
	}
	
	public int getCANum(int pID){
		String sql;
		ResultSet rs=null;
		int caNum=0;
		
		sql="select * from bankwanterlist where pID=" + pID+" limit 1";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				caNum=rs.getInt("caNum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return caNum;
	}
	
	public int getSize(){
		String sql;
		ResultSet rs=null;
		int sum=0;
		
		sql="select count(*) as sum from bankwanterlist";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				sum=rs.getInt("sum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sum;
	}
}
